<template>
  <div class="">
    <van-nav-bar
      @click-left="$router.back()"
      left-arrow
      title="团队管理"
      fixed
    ></van-nav-bar>
    <h3 class="h3">
      <img src="../../assets/images/detail/head.png" alt="" />
      管理员统计
    </h3>
    <header>
      <div class="item">
        <div class="left">
          <img src="../../assets/images/admin/nav1.png" alt="" />
        </div>
        <div class="right">
          <h4>管理员总数</h4>
          <p>
            <strong>{{ comeObj.roleSum }}</strong
            >人
          </p>
        </div>
      </div>
      <div class="item">
        <div class="left">
          <img src="../../assets/images/admin/nav2.png" alt="" />
        </div>
        <div class="right">
          <h4>总客户数</h4>
          <p>
            <strong>{{ comeObj.sum }}</strong
            >人
          </p>
        </div>
      </div>
      <div class="item">
        <div class="left">
          <img src="../../assets/images/admin/nav3.png" alt="" />
        </div>
        <div class="right">
          <h4>今日新增客户</h4>
          <p>
            <strong>{{ comeObj.todayCount }}</strong
            >人
          </p>
        </div>
      </div>
    </header>
    <div class="info">
      <div class="item">
        <h4>总转发数</h4>
        <p>
          {{ comeObj.sumTransmit || 0 }}
          <span>篇</span>
        </p>
      </div>
      <div class="item">
        <h4>总阅读数</h4>
        <p>{{ comeObj.sumRead || 0 }} <span>篇</span></p>
      </div>
      <div class="item">
        <h4>今日转发</h4>
        <p>{{ comeObj.todayTransmit || 0 }}<span>篇</span></p>
      </div>
      <div class="item">
        <h4>今日阅读</h4>
        <p>{{ comeObj.todayRead || 0 }}<span>篇</span></p>
      </div>
    </div>
    <div class="poster_type">
      <ul>
        <li :class="{ active: type == 1 }" @click="setType(1)">
          管理员列表
          <i></i>
        </li>
        <li :class="{ active: type == 2 }" @click="setType(2)">
          申请列表
          <i></i>
        </li>
      </ul>
    </div>
    <main>
      <div class="one" v-if="type == 1 && roleList.length !== 0">
        <van-list
          v-model="loading"
          :finished="finished"
          :immediate-check="false"
          @load="onLoad"
        >
          <div
            class="item"
            v-for="item in roleList"
            :key="item.id"
            @click="$router.push('/adminDetail?id=' + item.id)"
          >
            <div class="top">
              <div class="left">
                <img :src="$publicObj.urlImg(item.image)" alt="" />
              </div>
              <div class="center">
                <h3>{{ item.name }}</h3>
                <div class="foot">
                  <p>
                    <img src="../../assets/images/form/phone.png" alt="" />
                    {{ item.phone }}
                  </p>
                </div>
              </div>
              <div class="right">
                <p>
                  <van-switch
                    v-model="item.roleStatus"
                    size="12px"
                    :active-value="1"
                    :inactive-value="2"
                    @click.stop="updateStatus(item.roleStatus, item.id)"
                  />
                  &nbsp;{{ item.roleStatus == 2 ? "禁用" : "启用" }}
                </p>
                <span>总客户数:{{ item.clientCount }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>

      <div class="two" v-else-if="type == 2 && roleList.length !== 0">
        <van-list v-model="loading" :finished="finished" @load="onLoad">
          <div class="item" v-for="item in roleList" :key="item.id">
            <div class="top">
              <div class="left">
                <img :src="$publicObj.urlImg(item.image)" alt="" />
              </div>
              <div class="center">
                <h3>
                  {{ item.userName }}
                </h3>
                <div class="foot">
                  <p>
                    <img src="../../assets/images/form/phone.png" alt="" />
                    {{ item.phone }}
                  </p>
                  <span>{{ item.gmtCreate.slice(0, 11) }}</span>
                </div>
              </div>
              <div class="right">
                <img
                  v-if="item.status == 1"
                  src="../../assets/images/admin/pass.png"
                  alt=""
                />
                <img
                  v-else-if="item.status == 2"
                  src="../../assets/images/admin/nopass.png"
                  alt=""
                />
                <div v-else class="pass" @click="clickShow(item)">审核</div>
              </div>
            </div>
          </div>
        </van-list>
      </div>

      <van-empty description="暂无数据" v-else />
      <footer v-show="type == 1">
        <div class="btn" @click="$router.push('/addAdmin')">
          <van-icon name="add" style="margin-right:5px" />添加管理员
        </div>
      </footer>
    </main>
    <!-- 弹框 -->
    <van-dialog v-model="show" :show-confirm-button="false">
      <template #title>
        <van-icon name="cross" @click="show = false" />
      </template>
      <template #default>
        <div class="cont">
          <p>是否将该人员升级为管理员？</p>
          <van-divider />
          <div class="foot">
            <div class="right" @click="roleAudit(1)">通过</div>
            <div class="left" @click="roleAudit(2)">不通过</div>
          </div>
        </div>
      </template>
    </van-dialog>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      searchKey: "", //搜索关键字
      type: 1, //类型
      roleList: [], //申请列表
      index: "",
      show: false, //弹框
      itemObj: {}, //每一项记录
      loading: false, //加载状态
      finished: false, //完成
      page: 1, //页数
      limit: 8, //条数
      totalPage: 100, //总页数
      comeObj: {},
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    document.querySelector("html").style.backgroundColor = "#f3f5f7";
    document.querySelector("body").style.backgroundColor = "#f3f5f7";
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.getRoleList();
  },
  methods: {
    //触底下一页
    onLoad() {
      if (this.page >= this.totalPage) {
        this.finished = true;
        return;
      }
      this.page++;

      this.setType(this.type);
      // 加载状态结束
      this.loading = false;
    },
    //点击审核
    clickShow(item) {
      this.itemObj = item;
      this.show = true;
    },
    //启用禁用
    async updateStatus(roleStatus, userId) {
      var str;
      if (roleStatus == 1) {
        str = "禁用";
        roleStatus = 2;
      } else {
        str = "启用";
        roleStatus = 1;
      }
      let obj = {
        roleStatus,
        userId,
      };
      let res = await this.$api.roleChange(obj);
      if (res.code != 0) return this.$toast(res.msg);
      this.$toast(str + "成功");
    },
    // 切换
    setType(n) {
      // this.roleList = [];
      if (this.type != n) {
        this.roleList = [];
        this.page = 1;
        this.totalPage = "";
      }
      this.type = n;
      if (n == 1) {
        this.getRoleList();
      } else {
        this.applyRoleList();
      }
    },
    // 管理员列表
    async getRoleList() {
      let res = await this.$api.getRoleList({
        account: this.$store.state.userInfo.account,
        page: this.page,
        limit: this.limit,
      });
      if (res.code !== 0) return this.$toast(res.msg);
      //判断是否是第一页
      if (this.page == 1) {
        this.roleList = res.data.list || [];
      } else {
        this.roleList.push(...res.data.list);
      }
      this.totalPage = res.data.totalPage;
      this.comeObj = res.map;
    },
    // 申请列表
    async applyRoleList() {
      let obj = {
        account: this.$store.state.userInfo.account,
        page: this.page,
        limit: this.limit,
      };
      let res = await this.$api.roleList(obj);
      if (res.code !== 0) return this.$toast(res.msg);
      // this.roleList = res.data || [];
      //判断是否是第一页
      if (this.page == 1) {
        this.roleList = res.data.list || [];
      } else {
        this.roleList.push(...res.data.list);
      }
      this.totalPage = res.data.totalPage;
    },
    //审核
    async roleAudit(n) {
      let obj = {
        id: this.itemObj.id,
        userId: this.itemObj.userId,
        role: n,
      };
      let res = await this.$api.roleAudit(obj);
      if (res.code !== 0) return this.$toast(res.msg);
      this.$toast("审核成功");
      this.show = false;
      this.applyRoleList();
    },
  },
};
</script>
<style scoped lang="less">
/* @import url(); 引入css类 */
.h3 {
  padding-top: 66px;
  font-size: 16px;
  font-weight: 700;
  color: #333333;
  letter-spacing: 1px;
  margin: 0 0 10px 15px;
  img {
    width: 4px;
    height: 15px;
    margin-right: 5px;
  }
}
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 15px;
  background-color: #fff;
  border-radius: 4px;
  padding: 20px 0 17px;
  .item {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
    &:nth-of-type(n + 2) {
      border-left: 1px dashed #969798;
    }
    .left {
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 27px;
        height: 27px;
        margin-right: 7px;
      }
    }
    .right {
      h4 {
        font-size: 11px;
        color: #999999;
        margin-bottom: 4px;
      }
      p {
        font-size: 10px;
        color: #2d2d2d;
        letter-spacing: 1px;
        strong {
          font-size: 16px;
        }
      }
    }
  }
}
.info {
  display: flex;
  justify-content: space-between;
  margin: 15px;
  .item {
    width: 82px;
    height: 47px;
    border-radius: 5px;
    color: #fff;
    border-radius: 10px;
    background: url("../../assets/images/detail/bg1.png") no-repeat;
    background-size: 100%;
    &:nth-of-type(2) {
      background: url("../../assets/images/detail/bg2.png") no-repeat;
      background-size: 100%;
    }
    &:nth-of-type(3) {
      background: url("../../assets/images/detail/bg3.png") no-repeat;
      background-size: 100%;
    }
    &:nth-of-type(4) {
      background: url("../../assets/images/admin/bg4.png") no-repeat;
      background-size: 100%;
    }

    h4 {
      font-size: 10px;
      padding: 6px 0 3px;
      text-align: center;
    }
    p {
      font-size: 12px;
      font-weight: 700;
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        font-size: 8px;
        font-weight: normal;
      }
    }
  }
}
.poster_type {
  padding: 12px 100px;

  background-color: #fff;
  ul {
    display: -webkit-flex;
    display: flex;
    justify-content: space-between;
    li {
      font-size: 16px;
      color: #666;
      display: -webkit-flex;
      display: flex;
      justify-content: center;
      position: relative;
      align-items: center;
      padding: 5px 0;
      i {
        width: 28px;
        height: 3px;
        border-radius: 3px;
        position: absolute;
        bottom: -5px;
      }
    }
    .active {
      font-weight: 700;
      color: #000;
      i {
        background-color: #0264d1;
      }
    }
  }
}
main {
  padding: 0 15px 70px;

  .one {
    // height: calc(100vh - 160px);
    // overflow-y: scroll;
    .item {
      position: relative;
      padding: 14px 8px 9px;
      background-color: #ffffff;
      border-radius: 5px;
      box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.06);
      margin-top: 12px;
      .top {
        display: flex;
        width: 100%;
        .left {
          img {
            width: 53px;
            height: 53px;
          }
        }
        .center {
          flex: 1;
          margin-left: 11px;
          display: flex;
          flex-direction: column;
          justify-content: space-around;

          h3 {
            font-size: 14px;
            color: #333333;
          }
          .foot {
            display: flex;
            align-items: center;
            justify-content: space-between;
            p {
              font-size: 11px;
              color: #666666;
              display: flex;
              align-items: center;
              img {
                width: 15px;
                height: 15px;
                margin-right: 5px;
              }
            }
            span {
              font-size: 10px;
              color: #999999;
            }
          }
        }
        .right {
          font-size: 10px;
          color: #999999;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          span {
            margin-top: 14px;
            font-size: 10px;
            color: #999999;
          }
        }
      }
      .bottom {
        margin-top: 10px;
        font-size: 11px;
        color: #666666;
      }
      .badge {
        position: absolute;
        top: 0;
        right: 3px;
        display: flex;
        .abp {
          width: 32px;
          height: 20px;
        }
        .visit {
          width: 44px;
          height: 19px;
          margin-right: 10px;
        }
      }
    }
  }
  .two {
    // height: calc(100vh - 90px);
    // overflow-y: scroll;
    .item {
      position: relative;
      padding: 14px 8px 9px;
      background-color: #ffffff;
      border-radius: 5px;
      box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.06);
      margin-top: 12px;
      .top {
        display: flex;
        width: 100%;
        .left {
          img {
            width: 53px;
            height: 53px;
          }
        }
        .center {
          flex: 1;
          margin-left: 11px;
          display: flex;
          flex-direction: column;
          justify-content: space-around;

          h3 {
            font-size: 14px;
            color: #333333;
          }
          .foot {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding-right: 20px;
            p {
              font-size: 11px;
              color: #666666;
              display: flex;
              align-items: center;
              justify-content: space-between;

              img {
                width: 15px;
                height: 15px;
                margin-right: 5px;
              }
            }
            span {
              font-size: 10px;
              color: #999999;
            }
          }
        }
        .right {
          display: flex;
          justify-content: center;
          align-items: center;
          .pass {
            width: 43px;
            height: 19px;
            background-color: #477fe6;
            box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
            border-radius: 2px;
            font-size: 12px;
            color: #ffffff;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          img {
            width: 45px;
          }
        }
      }
      .bottom {
        margin-top: 10px;
        font-size: 11px;
        color: #666666;
      }
      .badge {
        position: absolute;
        top: 0;
        right: 3px;
        display: flex;
        .abp {
          width: 32px;
          height: 20px;
        }
        .visit {
          width: 44px;
          height: 19px;
          margin-right: 10px;
        }
      }
    }
  }
  footer {
    position: fixed;
    left: 0;
    bottom: 0px;
    height: 40px;
    width: 100%;
    padding: 15px 0;
    background-color: #fff;
    display: flex;
    justify-content: center;
    .btn {
      color: #fff;
      width: 145px;
      height: 40px;
      background-color: #477fe6;
      border-radius: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 13px;
      color: #ffffff;
    }
  }
}
/deep/ .van-dialog__header {
  text-align: right;
  padding: 15px 15px 0 0;
}
.van-dialog {
  width: 272px;
  .cont {
    font-size: 14px;
    font-weight: 700;
    color: #353535;
    text-align: center;
    p {
      padding: 20px 0;
    }
    .van-divider {
      margin: 0;
    }
    .foot {
      width: 100%;
      height: 43px;
      display: flex;
      .left,
      .right {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: #999999;
      }
      .right {
        color: #009dff;
        border-left: 1px solid #f5f6f8;
      }
    }
  }
}
</style>
